/*
 * Lab5D.cpp
 *
 *  Created on: 2012-01-08
 *      Author: Anthony Varghese
 * Description: Simple program to see how dynamic memory allocation
 *              works in a linked structure.
 */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct ListNode{
	string item;
	int count;
	ListNode* link;
};

int main(){
	char filename[] = "src/inputdata";
	cout << " Reading from file: " << filename << " . . . ";

	ifstream in( filename );
	if (in.fail()){
		cout << " ... not able to read it! Exiting." << endl;
		return -1;
	}
	cout << " ... OK, file opened." << endl << endl;

	ListNode* first = new ListNode();
	ListNode* p = first;
	int count = 1;

	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p->link = new ListNode();
	p = p->link;
	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p->link = new ListNode();
	p = p->link;
	if (in.eof()) return -1;
	getline( in, p->item );
	p->count = count++;

	p = first;
	cout << p->count << ": " << p->item << endl;
	p = p->link;
	cout << p->count << ": " << p->item << endl;
	p = p->link;
	cout << p->count << ": " << p->item << endl;
}



